/**
 * Created by fky on 4/10/2018.
 */

import {Injectable} from '@angular/core';
import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {Observable} from 'rxjs/observable';
import {UserModel} from '../models/user.model';
import {NbAuthService} from '@nebular/auth/services/auth.service';
import {NbAuthJWTToken} from '@nebular/auth';


@Injectable()
export class ShareService {

  private dataSource = new BehaviorSubject<UserModel>(new UserModel());


  private data = this.dataSource.asObservable();

  constructor(private authService: NbAuthService) {
    this.authService.getToken().subscribe((token: NbAuthJWTToken) => {
      const val = token.getPayload();
      this.updateData(new UserModel({userName: val.user_name, id: val.user_id}));
    });
  }


  getData(): Observable<UserModel> {
    return this.data;
  }

  updateData(val: UserModel) {
    this.dataSource.next(val);
  }

}
